package com.thinvent.nj.ais.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.thinvent.nj.common.rest.ResponseEntity;
import com.thinvent.nj.common.util.MapperUtil;
import com.thinvent.nj.ais.entity.DictGroup;
import com.thinvent.nj.ais.entity.DictItem;
import com.thinvent.nj.ais.service.DictService;
import com.thinvent.nj.web.controller.BaseViewController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
* 字典管理
* 自动生成的空白页字典管理
* @author liupj
* @date 2018-3-27
*/
@Controller
@RequestMapping(path = "/sys/dicts")
public class DictController extends BaseViewController {

    @Autowired
    private DictService dictService;

    @RequiresPermissions("dict:view")
    @RequestMapping(method = RequestMethod.GET)
    public String toHtml() {
        return "sys/dictList";
    }

    @RequestMapping(path = "/groups", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity findListGroupData() {
       List<DictGroup> targets = dictService.getAll();
        return ResponseEntity.ok(targets);
    }

    @RequestMapping(path = "/groups/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity get(@PathVariable("id") String id) {
        return ResponseEntity.ok(dictService.get(id));
    }

    @RequestMapping(path = "/groups", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity insert(@RequestBody Map<String, Object> params) {
        DictGroup target = new DictGroup();
        MapperUtil.copy(params, target);

        dictService.insert(target);

        return ResponseEntity.ok();
    }

    @RequestMapping(path = "/groups/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody Map<String, Object> params) {
       DictGroup target = dictService.get(id);
        MapperUtil.copy(params, target);

        dictService.update(target);

        return ResponseEntity.ok();
    }

    @RequestMapping(path = "/groups/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseEntity del(@PathVariable("id") String id) {
        dictService.delete(id);
        return ResponseEntity.ok();
    }

    @RequestMapping(path = "/groups/{groupId}/items/search/page", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity findTableData(@PathVariable("groupId") String groupId) {
        List<DictItem> targets = dictService.getItemsByGroupId(groupId);

        JSONObject jsonObject = new JSONObject(1);
        jsonObject.put("result", targets);

        return ResponseEntity.ok(jsonObject);
    }


    @RequestMapping(path = "/groups/{groupId}/items", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity insertDictItem(@PathVariable("groupId") String groupId, @RequestBody Map<String, Object> params) {
        DictItem target = new DictItem();
        target.setGroupId(groupId);

        MapperUtil.copy(params, target);

        dictService.insertDictItem(target);

        return ResponseEntity.ok();
    }

    @RequestMapping(path = "/groups/{groupId}/items/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getDictItem(@PathVariable("groupId") String groupId, @PathVariable("id") String id) {
        return ResponseEntity.ok(dictService.getDictItem(id));
    }

    @RequestMapping(path = "/groups/{groupId}/items/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity updateDictItem(@PathVariable("groupId") String groupId, @PathVariable("id") String id, @RequestBody Map<String, Object> params) {
        DictItem target = dictService.getDictItem(id);
        target.setGroupId(groupId);

        MapperUtil.copy(params, target);

        dictService.updateDictItem(target);

        return ResponseEntity.ok();
    }


    @RequestMapping(path = "/groups/{groupId}/items/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseEntity delDictItem(@PathVariable("groupId") String groupId, @PathVariable("id") String id) {
        dictService.deleteDictItem(id);
        return ResponseEntity.ok();
    }

    @RequestMapping(path = "/groups/keys/{groupKey}/items", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity findDictItems(@PathVariable("groupKey") String groupKey) {
        List<DictItem> targets = dictService.getItemsByGroupKey(groupKey);
        return ResponseEntity.ok(targets);
    }
}
